<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\User;

use App\Models\DividendAccount;
use App\Models\DividendTransaction;
use App\Service\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class DividendAccountServices extends BaseService
{
    /**
     * 充值
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $type
     * @param mixed $describe
     * @return Builder|Builder[]|Collection|false|Model|void
     */
    public function recharge($user_id, $amount, $type = 1, $describe = '充值')
    {
        if ($amount < 0) {
            return false;
        }
        if ($describe == '充值') {
            $describe = $type == 1 ? '积分' : '通证';
            $describe .= '分红账户充值:' . $amount;
        }
        $account = DividendAccount::query()->where('user_id', $user_id)->firstOrCreate([
            'user_id' => $user_id,
        ]);
        if ($type == 1) {
            $account->point_amount += $amount;
            $account->point_total_number += $amount;
            $now_amount = $account->point_amount;
        } else {
            $account->bean_amount += $amount;
            $account->bean_total_number += $amount;
            $now_amount = $account->bean_amount;
        }

        $account->save();
        $this->transactionHistory($user_id, $amount, $now_amount, $describe, $type, 1);
        return $account;
    }

    /**
     * 分红转换.
     *
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $type
     * @param mixed $describe
     * @return Builder|Builder[]|Collection|false|Model|void
     */
    public function consumption($user_id, $amount, $type = 1, $describe = '分红转换')
    {
        $account = DividendAccount::query()->where('user_id', $user_id)->firstOrCreate([
            'user_id' => $user_id,
        ]);
        if ($type == 1 && $account->point_amount - $amount < 0) {
            return false;
        }
        if ($type == 2 && $account->bean_amount - $amount < 0) {
            return false;
        }
        if ($describe == '分红转换') {
            $describe = $type == 1 ? '积分' : '通证';
            $describe .= '分红转换:' . $amount;
        }
        if ($type == 1) {
            $account->point_amount -= $amount;
            $account->point_dividend_amount += $amount;
            $now_amount = $account->point_amount;
        } else {
            $account->bean_amount -= $amount;
            $account->bean_dividend_amount += $amount;
            $now_amount = $account->bean_amount;
        }
        $account->save();
        $this->transactionHistory($user_id, $amount, $now_amount, $describe, $type, 2);
        return $account;
    }

//    是否足够

    /**
     * 交易记录.
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $balance
     * @param mixed $transaction_type
     * @param mixed $describe
     * @param mixed $change_type
     * @param mixed $type
     */
    private function transactionHistory($user_id, $amount, $balance, $describe, $type, $change_type)
    {
        DividendTransaction::query()->create([
            'user_id' => $user_id,
            'amount' => $amount,
            'balance' => $balance,
            'describe' => $describe,
            'change_type' => $change_type,
            'type' => $type,
        ]);
    }
}
